package com.smartleanx.module.hrm.controller.admin.dept;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.util.collection.CollectionUtils;
import com.smartleanx.framework.common.util.object.BeanUtils;
import com.smartleanx.module.hrm.controller.admin.dept.vo.DeptListRespVo;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.eums.employee.EmployeeStatusEnum;
import com.smartleanx.module.hrm.intergration.DeptServiceProvider;
import com.smartleanx.module.hrm.service.employee.EmployeeService;
import com.smartleanx.module.system.api.dept.dto.DeptRespDTO;
import com.smartleanx.module.system.api.dept.dto.SystemFrameVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.Collections;
import java.util.List;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.framework.common.pojo.CommonResult.success;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.DEPT_EXISTS_EMPLOYEE;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.DEPT_EXISTS_CHILD;

@Tag(name = "管理后台 - 劳安心部门")
@RestController
@RequestMapping("/hrm/dept")
@Validated
public class DeptController {

    @Resource
    private DeptServiceProvider deptServiceProvider;

    @Resource
    private EmployeeService employeeService;

    @DeleteMapping("/delete")
    @Operation(summary = "删除部门")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:dept:delete')")
    public CommonResult<Boolean> deleteDept(@RequestParam("id") Long id) {
        List<EmployeeDO> empList = employeeService.getEmployeeListByDeptId(Collections.singletonList(id), null);
        if (CollUtil.isNotEmpty(empList)) {
            throw exception(DEPT_EXISTS_EMPLOYEE);
        }
        List<DeptRespDTO> childDeptList = deptServiceProvider.getChildDeptList(id);
        if (CollUtil.isNotEmpty(childDeptList)) {
            throw exception(DEPT_EXISTS_CHILD);
        }
        deptServiceProvider.deleteDept(id);
        return success(true);
    }

    @GetMapping("/list-tree")
    @Operation(summary = "获取组织架构左侧树")
    @PreAuthorize("@ss.hasPermission('system:dept:query')")
    public CommonResult<List<DeptListRespVo>> getDeptTreeList() {
        List<DeptRespDTO> treeList = deptServiceProvider.getDeptTreeList();
        List<DeptListRespVo> respVos = BeanUtils.toBean(treeList, DeptListRespVo.class);
        if (CollUtil.isNotEmpty(respVos)) {
            List<EmployeeDO> employeeDOS = employeeService.getAllEmployee();
            if (CollUtil.isNotEmpty(employeeDOS)) {
                employeeDOS.removeIf(e -> e.getState().equals(EmployeeStatusEnum.SEPARATED.getCode()));
                for (DeptListRespVo vo : respVos) {
                    List<Long> allIds = CollectionUtils.convertList(deptServiceProvider.getChildDeptList(vo.getId()), DeptRespDTO::getId);
                    vo.setCount(employeeDOS.stream().filter(e -> allIds.contains(e.getDeptId()) || vo.getId().equals(e.getDeptId())).count());
                }
            }
        }
        return success(respVos);
    }
    @GetMapping("/getFrameByDepartmentId")
    @Operation(summary = "组织结构左侧树列表")
    CommonResult<SystemFrameVo> getFrameByDepartmentId() {
        return deptServiceProvider.getFrameByDepartmentId();
    }
}
